<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\response\Json;

/**
 * 基本Json响应
 * @param int $code 响应Code  0->处理成功
 * @param string $message 响应信息
 * @param mixed $data 响应数据
 * @return Json
 */
function baseResponse($code, $message, $data = null)
{
    return json([
        'code' => $code,
        'message' => $message,
        'data' => $data
    ]);
}

/**
 * 将Str转换为Hash
 * @param string $str 待转换的字符串
 * @return false|string|null
 */
function strHash($str)
{
    return password_hash($str, PASSWORD_DEFAULT);
}

/**
 * 验证Str和Hash是否匹配
 * @param string $str Str
 * @param string $hash Hash值
 * @return bool
 */
function strVery($str, $hash)
{
    return password_verify($str, $hash);
}

/**
 * 获取uri
 * @param string $url request获取的url
 * @return string
 */
function getUri($url)
{
    $uri = explode('/', $url);
    if (count($uri) == 5) {
        unset($uri[count($uri) - 1]);
    }
    return implode($uri, '/');
}

/**
 * 获取uri
 * @param string $uri getUri的uri
 * @return string
 */
function getControllerAndMethod($uri)
{
    $uri = explode('/', $uri);
    return $uri[count($uri) - 2] . '/' . $uri[count($uri) - 1];
}
